cmake_minimum_required(VERSION 3.10)

# set the project name
project(ffmpeg_wasm VERSION 0.1)

option(BUILD_WASM "编译wasm版本" ON)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
#set(CMAKE_CXX_FLAGS "")
#set(CMAKE_EXE_LINKER_FLAGS "")

# https://cmake.org/cmake/help/latest/command/configure_file.html
configure_file(config.h.in include/config.h)

#set(SOURCES
#        src/*.cpp
#        )
if (BUILD_WASM)
    message(">> wasm version")
    set(FFMPEG /usr/local/etc/ffmpeg_em)
    set(SOURCES "src/decode_wasm.cpp" "src/decode.cpp")
    set(CMAKE_EXECUTABLE_SUFFIX ".html")
    set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

    add_executable(${PROJECT_NAME} ${SOURCES})

    set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS " \
    -O3 \
    -s EXIT_RUNTIME=0 \
    -gsource-map \
    -s INITIAL_MEMORY=30mb \
    -s ALLOW_MEMORY_GROWTH=1 \
    --source-map-base http://127.0.0.1:8081/bin/ \
    -s EXPORTED_FUNCTIONS=_ffmpeg_init,_ffmpeg_release,_ffmpeg_input_data,_ffmpeg_decode_frame,_ffmpeg_dump \
    -s EXPORTED_RUNTIME_METHODS=ccall,cwrap,addFunction \
    -s NO_DISABLE_EXCEPTION_CATCHING \
")
    #    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wlimited-postlink-optimizations ")

else ()
    message(">> normal version")
    set(FFMPEG /usr/local/etc/ffmpeg)
    # aux_source_directory(./ SOURCES)
    file(GLOB SOURCES "src/*.cpp" "src/*.c")

    add_executable(${PROJECT_NAME} ${SOURCES})
endif ()

# 指定项目编译的时候需要include的文件路径，PROJECT_BINARY_DIR变量为编译发生的目录，也就是make执行的目录，PROJECT_SOURCE_DIR为工程所在的目录
# https://cmake.org/cmake/help/latest/command/target_include_directories.html
target_include_directories(${PROJECT_NAME} PUBLIC
        "${PROJECT_BINARY_DIR}/include"
        "${FFMPEG}/include"
        )

target_link_directories(${PROJECT_NAME} PUBLIC
        "${FFMPEG}/lib"
        )

if (BUILD_WASM)
    target_link_directories(${PROJECT_NAME} PUBLIC
            "lib"
            )

    target_link_libraries(${PROJECT_NAME} PUBLIC
            "swscale"
            "avformat"
            "avcodec"
            "avutil"
            "pthread"
            "avfilter"
            "postproc"
            "swresample"
            "-lz"
            "-lm"
            )
else ()
    target_link_libraries(${PROJECT_NAME} PUBLIC
            "swscale"
            "avformat"
            "avcodec"
            "avutil"
            "pthread"
            "avfilter"
            "postproc"
            "swresample"
            "-lz"
            "-lm"
            "SDL2"
            )
endif ()


